package com.zzyweb.rentalcarsys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyweb.rentalcarsys.entity.Car;
import com.zzyweb.rentalcarsys.mapper.CarMapper;
import com.zzyweb.rentalcarsys.service.CarService;
import com.zzyweb.rentalcarsys.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author zhiyi
 * @date 10/10/25
 **/
@Service
public class CarServiceImpl implements CarService {
    @Autowired
    CarMapper carMapper;
    @Override
    public PageResult<Car> getAllCar(Long currentPage, Long pageSize, Car car) {
        Page<Car> page = new Page<>(currentPage,pageSize);
        if (car == null) {
            List<Car> cars = carMapper.selectList(page, null);
            return new PageResult<>(page.getCurrent(), page.getSize(),page.getTotal(), cars);
        } else {
            QueryWrapper<Car> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(car.getCarType() != null, "car_type", car.getCarType())
                    .like(StringUtils.hasText(car.getCarMake()),"car_make",car.getCarMake())
                    .like(StringUtils.hasText(car.getCarModel()),"car_model",car.getCarModel())
                    .eq(StringUtils.hasText(car.getCarPlateNumber()),"car_plate_number",car.getCarPlateNumber())
                    .eq(car.getState()!=null,"state",car.getState())
                    .lt(car.getCarMileage()!=null, "car_mileage", car.getCarMileage());
            List<Car> cars = carMapper.selectList(page, queryWrapper);
            return new PageResult<>(page.getCurrent(),page.getSize(),page.getTotal(),cars);
        }
    }

    @Override
    public void addCar(Car car) {
        carMapper.insert(car);
    }

    @Override
    public void modifyCar(Car car) {
        carMapper.updateById(car);
    }

    @Override
    public void deleteCar(String carId) {

    }
}
